﻿unit PayIn;

interface

uses
  Winapi.Windows, Winapi.Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIFrame, DB, DBClient, ClientRowSet, uniScreenMask,
  uniBasicGrid, uniDBGrid, uniMultiItem, uniComboBox, uniDBComboBox,
  uniDBLookupComboBox, uniButton, uniBitBtn, uniDateTimePicker, uniLabel,
  uniGUIBaseClasses, uniPanel, uniPageControl, Clipbrd;

type
  TFrmPayIn = class(TUniFrame)
    UniPanel1: TUniPanel;
    UniLabel1: TUniLabel;
    edStartDate: TUniDateTimePicker;
    btFind: TUniBitBtn;
    gridOrder: TUniDBGrid;
    UniPanel3: TUniPanel;
    dsOrder: TDataSource;
    ScreenMask: TUniScreenMask;
    crsOrder: TClientRowSet;
    UniBitBtn1: TUniBitBtn;
    UniBitBtn2: TUniBitBtn;
    UniBitBtn3: TUniBitBtn;
    UniLabel2: TUniLabel;
    edCloseDate: TUniDateTimePicker;
    pnMoney_1: TUniPanel;
    UniLabel4: TUniLabel;
    dsEmp: TDataSource;
    cbNode: TUniDBLookupComboBox;
    dsNode: TDataSource;
    btExport: TUniBitBtn;
    crsOrderORDER_ID: TStringField;
    crsOrderCON_ORDID: TStringField;
    crsOrderBILL_NO: TStringField;
    crsOrderORDER_NO: TStringField;
    crsOrderORDER_TYPE: TStringField;
    crsOrderTSP_STATE: TIntegerField;
    crsOrderACPT_STATE: TIntegerField;
    crsOrderCARGO: TStringField;
    crsOrderCARGO_CNT: TIntegerField;
    crsOrderWEIGHT: TFloatField;
    crsOrderVOLUME: TStringField;
    crsOrderDISTANCE: TFloatField;
    crsOrderCOD: TFloatField;
    crsOrderCOD_FEE: TFloatField;
    crsOrderCOD_BLCID: TStringField;
    crsOrderCOD_STATE: TIntegerField;
    crsOrderASK_RTBILL: TIntegerField;
    crsOrderSEND_PNTNO: TStringField;
    crsOrderSEND_GROUP: TStringField;
    crsOrderSEND_NODE: TStringField;
    crsOrderSEND_NAME: TStringField;
    crsOrderSEND_TEL: TStringField;
    crsOrderSEND_CONT: TStringField;
    crsOrderSEND_CITY: TStringField;
    crsOrderSEND_ADDR: TStringField;
    crsOrderDEST_PNTNO: TStringField;
    crsOrderDEST_GROUP: TStringField;
    crsOrderDEST_NODE: TStringField;
    crsOrderDEST_NAME: TStringField;
    crsOrderDEST_TEL: TStringField;
    crsOrderDEST_CONT: TStringField;
    crsOrderDEST_CITY: TStringField;
    crsOrderDEST_ADDR: TStringField;
    crsOrderRECEIVER: TStringField;
    crsOrderTSP_ACCNO: TStringField;
    crsOrderTSP_PNTNO: TStringField;
    crsOrderTSP_GROUP: TStringField;
    crsOrderTSP_NODE: TStringField;
    crsOrderTSP_NAME: TStringField;
    crsOrderTSP_EMPTEL: TStringField;
    crsOrderTRUCK_NO: TStringField;
    crsOrderTSP_TEL: TStringField;
    crsOrderTSP_EMPNO: TStringField;
    crsOrderTSP_EMPNAME: TStringField;
    crsOrderTSP_USRNO: TStringField;
    crsOrderFETCH_NODE: TStringField;
    crsOrderFETCH_EMPNO: TStringField;
    crsOrderFETCH_EMPNAME: TStringField;
    crsOrderFETCH_EMPTEL: TStringField;
    crsOrderFETCH_NODE_INC: TFloatField;
    crsOrderDISP_NODE: TStringField;
    crsOrderDISP_EMPNO: TStringField;
    crsOrderDISP_EMPNAME: TStringField;
    crsOrderDISP_EMPTEL: TStringField;
    crsOrderDISP_NODE_INC: TFloatField;
    crsOrderRECBILL_NAME: TStringField;
    crsOrderFEE_BLCID: TStringField;
    crsOrderFEE: TFloatField;
    crsOrderPAYSIDE: TIntegerField;
    crsOrderADV_FEE: TFloatField;
    crsOrderINUS_FEE: TFloatField;
    crsOrderFEE_STATE: TIntegerField;
    crsOrderDLIV_PAYIN: TFloatField;
    crsOrderBACK_FEE: TFloatField;
    crsOrderCRT_DATE: TStringField;
    crsOrderSND_DATE: TStringField;
    crsOrderARV_DATE: TStringField;
    crsOrderRCV_TIME: TStringField;
    crsOrderEXCEPT_INFO: TStringField;
    crsOrderREMARK: TStringField;
    crsOrderGROUP_CODE: TStringField;
    crsOrderNODE_NO: TStringField;
    crsOrderNODE_NAME: TStringField;
    crsOrderSYS_ACCOUNT: TStringField;
    crsOrderUPDATETIME: TStringField;
    crsOrderVERSION: TLargeintField;
    crsOrderPAYFEE: TFloatField;
    crsOrderPAYCOD: TFloatField;
    crsOrderPAYINS: TFloatField;
    crsOrderPAYADV: TFloatField;
    crsOrderPAYABLE: TFloatField;
    crsOrderUNPAID: TFloatField;
    crsOrderEMPNAME: TStringField;
    crsOrderEMPNO: TStringField;
    crsOrderCHOOSED: TBooleanField;
    crsOrderPAID: TFloatField;
    UniPanel2: TUniPanel;
    lbBillCnt: TUniLabel;
    lbPayable: TUniLabel;
    lbPaid: TUniLabel;
    lbUnPaid: TUniLabel;
    pnlClose: TUniPanel;
    btClose: TUniBitBtn;
    UniLabel3: TUniLabel;
    cbEmp: TUniDBLookupComboBox;
    UniLabel5: TUniLabel;
    lblMoney: TUniLabel;
    crsOrderLKPAYSIDE: TStringField;
    crsNode: TClientRowSet;
    crsEmp: TClientRowSet;
    procedure crsOrderCalcFields(DataSet: TDataSet);
    procedure UniFrameCreate(Sender: TObject);
    procedure btFindClick(Sender: TObject);
    procedure UniBitBtn1Click(Sender: TObject);
    procedure UniBitBtn2Click(Sender: TObject);
    procedure gridOrderDblClick(Sender: TObject);
    procedure UniBitBtn3Click(Sender: TObject);
    procedure crsOrderAfterApplyUpdates(Sender: TObject;
      var OwnerData: OleVariant);
    procedure btExportClick(Sender: TObject);
    procedure gridOrderColumnSort(Column: TUniDBGridColumn; Direction: Boolean);
    procedure gridOrderCellClick(Column: TUniDBGridColumn);
    procedure btCloseClick(Sender: TObject);
    procedure cbEmpKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure cbNodeCloseUp(Sender: TObject);
  private
    { Private declarations }
    currField: TField;
  public
    { Public declarations }

    procedure ShowDispatch(condition:String);
    procedure ChooseAll(choosed:Boolean);
    procedure ChooseOne;
    procedure ShowTotalInfo;
    procedure RefreshTotalInfo(Sender: TObject);
  end;

implementation

uses ExpModule,EditReceive,accmodule,DictModule, Main,MessageBox, Paid, uCommFunction;

{$R *.dfm}



procedure TFrmPayIn.btCloseClick(Sender: TObject);
begin
  MainForm.CloseOneTab(Self.Hint);
  //MainForm.CloseOneTab('业务员缴款');
end;

procedure TFrmPayIn.btExportClick(Sender: TObject);
var
  FileName:String;
begin
  if not crsorder.Active then exit;
  if crsorder.RecordCount > 0 then
  begin
    FileName := 'files\' + cbEmp.Text + '-缴款单.csv';
    Mainform.ExportExcel('',gridOrder,FileName);
  end
  else
  begin
    FrmMessageBox.ShowInfo(GetShowMsg('0027', '当前没有记录，不能导出空的数据文件'));
  end;

end;

procedure TFrmPayIn.btFindClick(Sender: TObject);
begin
  if (cbEmp.Text = '') and (dmExpress.UserInfo.IsCenter) then
  begin
    FrmMessageBox.ShowInfo(GetShowMsg('0081', '请先选择业务员再进行该操作'));
    exit;
  end;  

  self.ShowDispatch('');
  ShowTotalInfo;
  gridOrder.OnColumnSort := gridOrderColumnSort;
end;

procedure TFrmPayIn.cbEmpKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key in [VK_DELETE, VK_BACK] then
  begin
    cbEmp.KeyValue := '';
    cbEmp.ItemIndex := -1;
  end;
end;

procedure TFrmPayIn.cbNodeCloseUp(Sender: TObject);
begin
  cbEmp.KeyValue := '';
  cbEmp.ItemIndex := -1;
  crsEmp.Filtered := False;
  if cbNode.Text <> '' then
  begin
    crsEmp.Filter := 'node_no=' + QuotedStr(VarToStr(cbNode.KeyValue)) + ' AND state<>1';
    crsEmp.Filtered := True;
  end;
end;

procedure TFrmPayIn.ChooseAll(choosed: Boolean);
var
  I:Integer;
  payable:Double;
begin
  payable := 0;
  crsOrder.First;
  for I := 1 to crsOrder.RecordCount do
  begin
    if choosed then
    begin
      payable := payable + crsOrder.FieldByName('unpaid').AsFloat;
    end;
    crsOrder.Edit;
    crsOrder.FieldByName('choosed').AsBoolean := choosed;
    crsOrder.Post;
    crsOrder.Next;
  end;
  lblMoney.Caption := Floattostr(payable);
end;

procedure TFrmPayIn.ChooseOne;
var
  payable:Double;
begin
  if crsOrder.FieldByName('FEE_STATE').AsInteger = 1 then exit;

  crsOrder.Edit;
  crsOrder.FieldByName('choosed').AsBoolean := not crsOrder.FieldByName('choosed').AsBoolean;
  crsOrder.Post;

  payable := crsorder.FieldByName('unpaid').AsFloat;
  if crsOrder.FieldByName('choosed').AsBoolean then
    lblMoney.Caption := floattostr(strtofloat(lblMoney.Caption) + payable)
  else
    lblMoney.Caption := floattostr(strtofloat(lblMoney.Caption) - payable);

end;

procedure TFrmPayIn.crsOrderAfterApplyUpdates(Sender: TObject;
  var OwnerData: OleVariant);
begin
  lblMoney.Caption := '0';
end;

procedure TFrmPayIn.crsOrderCalcFields(DataSet: TDataSet);
var
  fee,advfee,cod,insfee:double;
  fetchno,dispno,empno:String;
  payside,state,codstate:Integer;
begin
  fetchno := dataset.FieldByName('FETCH_EMPNO').AsString;
  dispno := dataset.FieldByName('DISP_EMPNO').AsString;
  payside := dataset.FieldByName('PAYSIDE').AsInteger;
  state := dataset.FieldByName('TSP_STATE').AsInteger;
  codstate := dataset.FieldByName('COD_STATE').AsInteger;

  empno := VarToStr(cbEmp.KeyValue);
  fee := 0;
  cod := 0;
  insfee := 0;
  advfee := 0;

  if empno <> '' then
  begin
    if ((payside=1) and (empno=fetchno)) or ((payside=2) and (empno=dispno) and (state=9)) then
    begin
      fee := DataSet.FieldByName('fee').AsFloat;
      advfee := DataSet.FieldByName('adv_fee').AsFloat;
      insfee := DataSet.FieldByName('inus_fee').AsFloat;
    end;

    if (empno=dispno) and (codstate = 0) then
      cod := DataSet.FieldByName('cod').AsFloat;
  end
  else
  begin
    if DataSet.FieldByName('fetch_node').AsString = cbNode.KeyValue then  //dmExpress.UserInfo.NodeNo then
    begin
      empno := DataSet.FieldByName('fetch_empno').AsString;
      if payside = 1 then
      begin
        fee := DataSet.FieldByName('fee').AsFloat;
        advfee := DataSet.FieldByName('adv_fee').AsFloat;
        insfee := DataSet.FieldByName('inus_fee').AsFloat;      
      end;
    end
    else
    if DataSet.FieldByName('disp_node').AsString = cbNode.KeyValue then //dmExpress.UserInfo.NodeNo then
    begin
      if (payside = 2) and (state = 9) then
      begin
        empno := DataSet.FieldByName('disp_empno').AsString;
        fee := DataSet.FieldByName('fee').AsFloat;
        advfee := DataSet.FieldByName('adv_fee').AsFloat;
        insfee := DataSet.FieldByName('inus_fee').AsFloat;
      end;
      if codstate = 0 then
      begin
        cod := DataSet.FieldByName('cod').AsFloat;
        if empno = '' then
          empno := DataSet.FieldByName('disp_empno').AsString;
      end;
    end;

    {if payside = 1 then
    begin
      empno := DataSet.FieldByName('fetch_empno').AsString;
      fee := DataSet.FieldByName('fee').AsFloat;
      advfee := DataSet.FieldByName('adv_fee').AsFloat;
      insfee := DataSet.FieldByName('inus_fee').AsFloat;
    end
    else
    if (payside = 2) and (state = 9) then
    begin
      empno := DataSet.FieldByName('disp_empno').AsString;
      fee := DataSet.FieldByName('fee').AsFloat;
      advfee := DataSet.FieldByName('adv_fee').AsFloat;
      insfee := DataSet.FieldByName('inus_fee').AsFloat;
    end;
    //if (payside = 2) and (codstate = 0) then
    if codstate = 0 then
      cod := DataSet.FieldByName('cod').AsFloat; }
  end;
  if VarToStr(cbEmp.KeyValue) <> '' then
  begin
    dataset.FieldByName('EMPNO').AsString := cbEmp.KeyValue;
    dataset.FieldByName('EMPNAME').AsString := cbEmp.Text;
  end
  else
  begin
    DataSet.FieldByName('empno').AsString := empno;
    if dmDict.crsEmployee.Locate('emp_no', empno, []) then
      DataSet.FieldByName('empname').AsString := dmDict.crsEmployee.FieldByName('emp_name').AsString;
  end;
  DataSet.FieldByName('PAYFEE').AsFloat := fee;
  DataSet.FieldByName('PAYCOD').AsFloat := cod;
  DataSet.FieldByName('PAYINS').AsFloat := insfee;
  DataSet.FieldByName('PAYADV').AsFloat := advfee;

  DataSet.FieldByName('PAYABLE').AsFloat := fee + insfee+ advfee + cod;
  if DataSet.FieldByName('FEE_STATE').AsInteger = 1 then
  begin
    DataSet.FieldByName('PAID').AsFloat := DataSet.FieldByName('PAYABLE').AsFloat;
    DataSet.FieldByName('UNPAID').AsFloat := 0;
  end
  else
  begin
    DataSet.FieldByName('PAID').AsFloat := 0;
    DataSet.FieldByName('UNPAID').AsFloat := DataSet.FieldByName('PAYABLE').AsFloat;
  end;
end;

procedure TFrmPayIn.gridOrderCellClick(Column: TUniDBGridColumn);
begin
  currField := Column.Field;
end;

procedure TFrmPayIn.gridOrderColumnSort(Column: TUniDBGridColumn;
  Direction: Boolean);
begin
  SortColumn((gridOrder.DataSource.DataSet as TClientRowSet), Column.FieldName, Direction);
end;

procedure TFrmPayIn.gridOrderDblClick(Sender: TObject);
begin
  if currField <> nil then
    Clipboard.AsText := currField.AsString;
  self.ChooseOne;
end;

procedure TFrmPayIn.RefreshTotalInfo(Sender: TObject);
begin
  ShowTotalInfo;
end;

procedure TFrmPayIn.ShowDispatch(condition: String);
var
  orderid,sqltxt,stdate,cldate,empno:String;
begin
  if not dmExpress.UserInfo.IsCenter then
  if (VarIsNull(cbNode.KeyValue)) or (VarIsEmpty(cbNode.KeyValue)) then
    cbNode.KeyValue := dmExpress.UserInfo.NodeNo;
  stdate := formatdatetime('yyyy-MM-dd',edStartDate.DateTime) + ' 00:00:00';
  cldate := formatdatetime('yyyy-MM-dd',edCloseDate.DateTime) + ' 24:00:00';

  empno := VarToStr(cbemp.KeyValue);

  {condition := 'CRT_DATE>=' + quotedstr(stdate)
             + ' and CRT_DATE<=' + quotedstr(cldate)
             + ' and TSP_GROUP=' + quotedstr(dmexpress.UserInfo.GroupCode)
             + ' and (fetch_node='+quotedstr(cbnode.KeyValue)+' or disp_node='+quotedstr(cbnode.KeyValue)+')'
             + ' and ((FEE_STATE=0 and '
             + '      ((PAYSIDE=1 and FETCH_EMPNO=' + quotedstr(empno) + ')'
             + '    or (PAYSIDE=2 and DISP_EMPNO='  + quotedstr(empno) + ') )'     //运费未接结
             + '  or (cod>0 and TSP_STATE=9 and COD_STATE=0 '
             + '      and DISP_EMPNO='+ quotedstr(empno)+ ')))';    }               //货款未结


  condition := '(1=1)';
  condition := condition +
    ' AND ((payside=1 AND crt_date>=' + QuotedStr(stdate) + ' AND crt_date<=' + QuotedStr(cldate) + ') ' +
    ' OR (payside=2 AND rcv_time>=' + QuotedStr(stdate) + ' AND rcv_time<=' + QuotedStr(cldate) + '))';
  //crt_date>=' + QuotedStr(stdate) + ' AND crt_date<=' + QuotedStr(cldate);
  condition := condition +
    ' AND tsp_group=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
    ' AND (fetch_node=' + QuotedStr(cbNode.KeyValue) + ' OR disp_node=' + QuotedStr(cbNode.KeyValue) + ')' +
    ' AND ((fee_state=0 AND ((payside=1 ';
  if empno <> '' then
    condition := condition + ' AND fetch_empno=' + QuotedStr(empno);
  condition := condition + ') OR (payside=2';
  if empno <> '' then
    condition := condition + ' AND disp_empno=' + QuotedStr(empno);
  condition := condition + ')) OR (cod > 0 AND tsp_state=9 AND cod_state=0';
  if empno <> '' then
    condition := condition + ' AND disp_empno=' + QuotedStr(empno);
  condition := condition + ')))';


  sqltxt := 'select * from tsp_order where ' + condition ;
  crsOrder.Close;
  crsOrder.CommandText := sqltxt;
  crsOrder.Open;
  crsOrder.Filter := '';
  lblMoney.Caption := '0';

end;

procedure TFrmPayIn.ShowTotalInfo;
var
  I:Integer;
  PAYABLE,PAID,UNPAID:double;
begin
  PAYABLE := 0;
  PAID := 0;
  UNPAID := 0;

  crsOrder.First;
  for I := 1 to crsOrder.RecordCount do
  begin
    PAYABLE := PAYABLE + crsOrder.FieldByName('PAYABLE').AsFloat;
    PAID := PAID + crsOrder.FieldByName('PAID').AsFloat;
    crsOrder.Next;
  end;
  UNPAID := PAYABLE - PAID;

  lbPayable.Caption := '应缴款:' + floattostr(PAYABLE);
  lbPaid.Caption    := '已缴款:' + floattostr(PAID);
  lbUnPaid.Caption  := '未缴款:' + floattostr(UNPAID);
  lbBillCnt.Caption := '合计票数:' + Inttostr(crsOrder.RecordCount);

end;

procedure TFrmPayIn.UniBitBtn1Click(Sender: TObject);
begin
  self.ChooseAll(true);
end;

procedure TFrmPayIn.UniBitBtn2Click(Sender: TObject);
begin
  self.ChooseAll(False);
end;

procedure TFrmPayIn.UniBitBtn3Click(Sender: TObject);
var
  contactor:String;
begin
  if lblMoney.Caption = '0' then
  begin
    FrmMessageBox.ShowInfo(GetShowMsg('0028', '缴款金额为0，不需要进行记账操作'));
    exit;
  end;

  contactor := crsOrder.FieldByName('TSP_EMPNAME').AsString;
  dmacc.AppendReceive('EMPPAID','业务员缴款',contactor,strtofloat(lblMoney.Caption));

  dsorder.DataSet.Filter := 'choosed=true';
  FrmEditReceive.RecvType := 'payin';
  FrmEditReceive.dsOrder.DataSet := crsOrder;
  FrmEditReceive.pnMoney := StrToFloat(lblMoney.Caption);
  FrmEditReceive.edMoney.Enabled := False;
  FrmEditReceive.RefreshNotifyEnvent := RefreshTotalInfo;
  FrmEditReceive.ShowModal;
end;

procedure TFrmPayIn.UniFrameCreate(Sender: TObject);
begin
  SetCompoentLanguage(Self);
  crsNode.Data := dmDict.crsNode.Data;

  crsEmp.Data := dmDict.crsEmployee.Data;
  crsEmp.Filter := 'state<>1';
  crsEmp.Filtered := True;

  edStartDate.DateTime := date - 1;
  edCloseDate.DateTime := date;
  cbNode.KeyValue := dmexpress.UserInfo.NodeNo;
  cbNode.Enabled := dmExpress.UserInfo.IsCenter;
  SetFieldsSort(gridOrder);
  currField := nil;
  gridOrder.OnColumnSort := nil;
  pnlClose.Visible := dmExpress.UserInfo.isPcModel;
  cbNodeCloseUp(cbNode);
end;

end.
